package com.thank.validator;

import com.thank.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * description: 自定义校验器
 *
 * @author thank
 * 2018/2/19 2:01
 */

/**
 * 注意该类不用@Component来注解, 只要实现了ConstraintValidator, Spring会自动注入
 */
public class MyCustomerValidator implements ConstraintValidator<MyCustomerValid, Object> {

    // 可以注入spring容器的任何服务
    @Autowired
    private HelloService helloService;

    @Override
    public void initialize(MyCustomerValid constraintAnnotation) {
        System.out.println("my valid initialize");
    }


    /**
     * 校验的执行逻辑
     * @param value 传入的校验值
     * @param context 上下文
     * @return 校验成功\\失败
     */
    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        System.out.println("valid value is : " + value);
        return false;
    }
}
